#include "Motor.h"
#include "stm32f10x.h"
#include "Pwm.h"
#include <math.h>

void motor_init(void){
    
    pwm_init();
        	/*开启时钟*/
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);		//开启GPIOA的时钟
	
	/*GPIO初始化*/
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);						//将PA1和PA2引脚初始化为推挽输出
	
}


uint8_t config_rotation_direction(int16_t count){
    
    if(count>=0){
    
        GPIO_SetBits(GPIOA,GPIO_Pin_4);
        GPIO_ResetBits(GPIOA,GPIO_Pin_5);
        return 1;
    }else{
        GPIO_SetBits(GPIOA,GPIO_Pin_5);
        GPIO_ResetBits(GPIOA,GPIO_Pin_4);
        return 0;
    }

}


int16_t speed_up(int16_t count) {
    
    uint8_t forward = config_rotation_direction(count);
    int16_t result = increasePulse(count&0x7fff);
    if(forward){
        return result;
    }
    
    return result|0x8000;
}

int16_t speed_down(int16_t count){

    uint8_t forward = config_rotation_direction(count);
    int16_t result = reducePulse(count&0x7fff);
    if(forward){
        return result;
    }
    return result|0x8000;
}

